<?php

  require_once "Mail.php";
  
  function inviteUser($emails) {
    $return_string = "";

    if (!isset($_SESSION['user_name'])) {
      $return_string .= "<returncode>0</returncode>\n";
      $return_string .= "<errormessage>You're not logged in!</errormessage>\n";
      return $return_string;
    }
    
    $email_received = explode(',', $emails);
     
    $return_string .= "<returncode>1</returncode>\n";
    $return_string .= "<message>";
    foreach ($email_received as $email) {
      $query = "SELECT id FROM User where email='$email'";
      $result = mysql_query($query);
      if (mysql_num_rows($result) > 0) {
        $return_string .= "$email is already registered!\n";
      } else {
        $from = "COBL <sands.ntu@gmail.com>";
        $to = $email;
        $subject = "COBL Invitation";
        $body = "Hi,\n"
            . "Your have been invited to COBL by " . $_SESSION['user_name'] . "\n"
            . "To start using COBL please go to http://code.google.com/p/socialcobs"
            . "\n\n"
            . "COBL Team";
        $port = 587;
        $host = "smtp.gmail.com";
        $username = "sands.ntu";
        $password = "1234qweasz";


        $headers = array ('From' => $from,
                          'To' => $to,
                          'Subject' => $subject);

        $smtp = Mail::factory('smtp', array ('host' => $host,
                                             'auth' => true,
                                             'port' => $port,
                                             'username' => $username,
                                             'password' => $password));

        $mail = $smtp->send($to, $headers, $body);
        if (PEAR::isError($mail)) {
          $return_string .= $mail->getMessage() . "\n";
        } else {
          $return_string .= "Invitation sent to $email.\n";
        }
      }
    }
    $return_string .= "</message>";
    return $return_string;
  }

?>
